/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package principal;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Conectar {
     protected ResultSet rs;
     protected Connection con;
     protected Statement stmt;


    public void consultarSQL(String consulta)
    {

    String driver = "org.postgresql.Driver";
    String conneccion = "jdbc:postgresql://localhost:5432/BD4-2";

    String usuario = "postgres";
    String clave = "postgres";
    try{
    Class.forName(driver);
    con =  DriverManager.getConnection(conneccion, usuario , clave);
    stmt = con.createStatement();
    rs = stmt.executeQuery(consulta);
    }
    catch ( Exception e ){
    System.out.println(e.getMessage());
    }

    }

    public void cerrarConexion()
    {
        try {
            stmt.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(Conectar.class.getName()).log(Level.SEVERE, null, ex);
        }

    }



    public int autogenerar(String consulta)
    {

    String driver = "org.postgresql.Driver";
      String conneccion = "jdbc:postgresql://localhost:5432/BD4-2";

    String usuario = "postgres";
    String clave = "postgres";
    int codigo=1;
    try{
    Class.forName(driver);
    con =  DriverManager.getConnection(conneccion, usuario , clave);
    stmt = con.createStatement();
    rs = stmt.executeQuery(consulta);
        while (rs.next()) {
             codigo =rs.getInt(1)+1;
            }
    }
    catch ( Exception e ){
    System.out.println(e.getMessage());
    }
    return codigo;
    }


    public Connection getCon() {
        return con;
    }

    public void setCon(Connection con) {
        this.con = con;
    }

    public ResultSet getRs() {
        return rs;
    }

    public void setRs(ResultSet rs) {
        this.rs = rs;
    }

    public Statement getStmt() {
        return stmt;
    }

    public void setStmt(Statement stmt) {
        this.stmt = stmt;
    }



}
